package com.example.jh352160.surfingtv.thematic_details.customView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.FavoriteItem;
import com.example.jh352160.surfingtv.network.bean.FavoriteBean;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.FavoriteService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static android.R.attr.targetId;

/**
 * Created by jh352160 on 2016/8/1
 */

public class Favorite_button extends LinearLayout {

    Context context;
    Boolean isFavorite;
    ImageView favoriteButton;
    TextView favoriteCount;
    int contentId;

    public Favorite_button(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public Favorite_button(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.thematic_favorite_button, this);
        favoriteButton = (ImageView) findViewById(R.id.favorite_button);
        favoriteCount = (TextView) findViewById(R.id.favorite_count);
        favoriteCount.setText("" + 0);
        isFavorite = false;
        favoriteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                Retrofit retrofit = CommonRetrofit.getRetrofit();
                FavoriteService favoriteService = retrofit.create(FavoriteService.class);
                Call<FavoriteItem> call = favoriteService.getItem(new FavoriteBean(2, targetId));
                call.enqueue(new Callback<FavoriteItem>() {
                    @Override
                    public void onResponse(Call<FavoriteItem> call, Response<FavoriteItem> response) {
                        if (isFavorite) {
                            favoriteButton.setImageResource(R.drawable.thematic_unfavorite);
                            favoriteCount.setText("" + (Integer.parseInt(favoriteCount.getText().toString()) - 1));
                            Toast.makeText(context, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        } else {
                            favoriteButton.setImageResource(R.drawable.thematic_favorite);
                            favoriteCount.setText("" + (Integer.parseInt(favoriteCount.getText().toString()) + 1));
                            Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                        }
                        isFavorite = !isFavorite;
                    }

                    @Override
                    public void onFailure(Call<FavoriteItem> call, Throwable t) {

                    }
                });

            }
        });
    }

    public void setData(boolean isFavorite, int favoriteCount, int contentId) {
        if (isFavorite) {
            favoriteButton.setImageResource(R.drawable.thematic_favorite);
        } else {
            favoriteButton.setImageResource(R.drawable.thematic_unfavorite);
        }
        this.favoriteCount.setText(favoriteCount + "");
        this.contentId = contentId;
    }
}
